<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="画室名称" prop="studio">
        <el-input v-model="ruleForm.studio"></el-input>
      </el-form-item>
      <el-form-item label="所在省份" prop="provice">
        <el-input v-model="ruleForm.provice"></el-input>
      </el-form-item>
      <el-form-item label="画室联系人" prop=“contact“>
        <el-input v-model="ruleForm.contact" ></el-input>
      </el-form-item>
      <el-form-item label="电话号码"  prop="phone">
        <el-input v-model="ruleForm.phone" ></el-input>
      </el-form-item>
      <el-form-item label="email"  prop="email">
        <el-input v-model="ruleForm.email" ></el-input>
      </el-form-item>
      <el-form-item label="xls"  prop="xls">
        <el-input v-model="ruleForm.url" ></el-input>
        <el-upload
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          multiple
          :limit="3"
          :on-exceed="handleExceed"
          :file-list="fileList">
          <el-button size="small" type="primary">点击上传</el-button>
          <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
        </el-upload>
      </el-form-item>
      <el-form-item  prop="submit">
        <el-button type="primary" @click="submitForm('ruleForm')">确认修改</el-button>
        <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
      </el-form-item>


    </el-form>

  </div>

</template>
<script>
  //这里要俺需要引入，我不是一个对象
  import {isvalidPhone,isvalidEmail} from '../../api/validata'
  //定义一个全局的变量，谁用谁知道
  var validPhone=(rule, value,callback)=>{
      if (!value){
          callback(new Error('请输入电话号码'))
      }else  if (!isvalidPhone(value)){
        callback(new Error('请输入正确的11位手机号码'))
      }else {
          callback()
      }
  }

  export default {
    data() {
      return {
        ruleForm: {
          studio: '',
          provice:'',
          contact:'',
          phone:'',
          email:'',
          url:''
        },
        rules: {
          name: [
            { required: true, message: '输入画室名称', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          provice: [
            { required: true, message: '输入省份', trigger: 'blur' },
          ],
          contact: [
            { required: true, message: '输入联系老师', trigger: 'blur' },
          ],
          phone: [
            { required: true, trigger: 'blur', validator: validPhone }//这里需要用到全局变量
          ],
          email: [
            { required: true, message: '请输入email', trigger: 'blur' },//这里需要用到全局变量
            { type: 'email', message: 'email格式错误,请输入正确的email', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');//这里就是符合规则，然后去调对应的接口
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }

</script>